﻿using LevelEditor.Model.Entity;
using LevelEditor.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace LevelEditor.Model.IO
{
    public class SpriteSheetIO
    {
        // Fields
        public static string SPRITESHEET_LIST_XML = @"Assets\spriteSheets_list.xml";

        // Methods
        internal static List<string> LoadImagesUrlFromSpriteSheetFolder(int spriteSheetID)
        {
            throw new NotImplementedException();
        }

        public static List<SpriteSheet> LoadSpriteSheets()
        {
            FileInfo spriteSheetsListXml = new FileInfo(FileUtil.AppStartUpPath + SPRITESHEET_LIST_XML);
            List<SpriteSheet> spriteSheets = new List<SpriteSheet>();
            if (spriteSheetsListXml.Exists)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<SpriteSheet>));
                using (FileStream stream = new FileStream(FileUtil.AppStartUpPath + SPRITESHEET_LIST_XML, FileMode.Open, FileAccess.Read))
                {
                    spriteSheets = serializer.Deserialize(stream) as List<SpriteSheet>;
                    stream.Close();
                }
            }
            return spriteSheets;
        }

        public static void SaveSpriteSheets(List<SpriteSheet> spriteSheets)
        {
            XmlSerializer serializer = new XmlSerializer(spriteSheets.GetType());
            using (FileStream stream = new FileStream(FileUtil.AppStartUpPath + SPRITESHEET_LIST_XML, FileMode.Create, FileAccess.ReadWrite))
            {
                serializer.Serialize((Stream)stream, spriteSheets);
                stream.Close();
            }
        }
    }


}
